package IO4;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.EmptyFileFilter;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.SuffixFileFilter;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

/**
 * @description: IO_Commons01
 * 依赖包中的 FileUtils
 * FileUtils.sizeOf     文件大小
 * FileUtils.listFiles(new File("xx"),xx,xx) 目标目录，过滤后缀或非空文件，目录选项
 * FileFilterUtils.or() and() SuffixFileFilter("xx") DirectoryFileFilter.INSTANCE（子孙级）
 *
 * readFileToString( new File("xxx"),"xx") 文件对象.字符集     
 * readlines()  逐行读取，返回类型为list
 *
 *write() writeStringToFile(xx,xx,xx,xx) 目标文件，字符串，字符集，是否追加
 *
 * copyXXXXXXX() 拷贝
 * copyFile(xx,xx) 源文件，目标文件
 * copyFileToDirectory() 源文件，目标目录
 *
 * @date: 2019/12/19 16:34
 * @author: Vicuna
 */


public class IO_Commons01 {
    public static void main(String[] args) throws IOException {
        long len = FileUtils.sizeOf(new File("src/IO4"));
        System.out.println(len);

        Collection<File> files = FileUtils.listFiles(new File("src/IO4"),
                FileFilterUtils.or(new SuffixFileFilter("java"),EmptyFileFilter.NOT_EMPTY) ,
                DirectoryFileFilter.INSTANCE);
        for (File file : files) {
            System.out.println(file.getAbsolutePath());
        }
        String msg=FileUtils.readFileToString(new File("src/IO4/aaa.txt"),"UTF-8");
        System.out.println(msg);
        List<String> msgs=FileUtils.readLines(new File("src/IO4/aaa.txt"),"UTF-8");
        System.out.println(msgs);
        String str="好好学习，天天向上！";
        FileUtils.write(new File("src/IO4/bbb.txt"),str,"UTF-8",false);

        FileUtils.copyFile(new File("src/img.jpg"),new File("src/IO4/img-copy.jpg"));
        FileUtils.copyFileToDirectory(new File("src/img.jpg"),new File("src/IO4"));

    }
}
